import { Router } from 'express'
import ArticleDao from '../dao/ArticleDao.js'
import Article from '../verify/Article.js'
import File from '../util/File.js'
import path from 'path'

let router = Router()

router.route('/api/article')
    .post(async (req, res) => {
        req.body.user_id = 1
        let article = Article.insertObj(req.body)
        if (!article) return res.end(JSON.stringify({ code: 500, msg: '参数有误' }))

        let result = await ArticleDao.insert(article)
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        //删除所有临时文章封面图
        File.delete(req.session.cover_imgs, req.body.cover_imgs)
        req.session.cover_imgs = []

        //删除所有临时文章内容图
        File.delete(req.session.article_imgs, req.body.article_imgs)
        req.session.article_imgs = []

        //删除所有临时文章内容视频
        File.delete(req.session.article_videos, req.body.article_videos)
        req.session.article_videos = []

        return res.end(JSON.stringify({ code: 200 }))
    })

    .delete(async (req, res) => {
        let article = Article.deleteObj(req.body)
        if (!article) return res.end(JSON.stringify({ code: 500, msg: '参数有误' }))

        let { cover_imgs, article_imgs, article_videos } = await ArticleDao.selectById(article.id)

        let result = await ArticleDao.delete(article)
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        //删除文章封面图
        for (const cover_img of cover_imgs) {
            let urls = cover_img.split('/')
            let old_url = path.join(path.resolve(), '/public/assets', 'img', urls[urls.length - 1])
            req.session.cover_imgs.push(old_url)
        }
        File.delete(req.session.cover_imgs)
        req.session.cover_imgs = []

        //删除文章内容图
        for (const article_img of article_imgs) {
            let urls = article_img.split('/')
            let old_url = path.join(path.resolve(), '/public/assets', 'img', urls[urls.length - 1])
            req.session.article_imgs.push(old_url)
        }
        File.delete(req.session.article_imgs)
        req.session.article_imgs = []

        //删除文章内容视频
        for (const article_video of article_videos) {
            let urls = article_video.split('/')
            let old_url = path.join(path.resolve(), '/public/assets', 'video', urls[urls.length - 1])
            req.session.article_videos.push(old_url)
        }
        File.delete(req.session.article_videos)
        req.session.article_videos = []

        return res.end(JSON.stringify({ code: 200 }))
    })

    .put(async (req, res) => {
        let article = Article.updateObj(req.body)
        if (!article) return res.end(JSON.stringify({ code: 500, msg: '参数有误' }))

        let { cover_imgs, article_imgs, article_videos } = await ArticleDao.selectById(article.id)

        let result = await ArticleDao.update(article)
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        //删除所有临时文章封面图
        for (const cover_img of cover_imgs) {
            let urls = cover_img.split('/')
            let old_url = path.join(path.resolve(), '/public/assets', 'img', urls[urls.length - 1])
            req.session.cover_imgs.push(old_url)
        }
        File.delete(req.session.cover_imgs, req.body.cover_imgs)
        req.session.cover_imgs = []

        //删除所有临时文章内容图
        for (const article_img of article_imgs) {
            let urls = article_img.split('/')
            let old_url = path.join(path.resolve(), '/public/assets', 'img', urls[urls.length - 1])
            req.session.article_imgs.push(old_url)
        }
        File.delete(req.session.article_imgs, req.body.article_imgs)
        req.session.article_imgs = []

        //删除所有临时文章内容视频
        for (const article_video of article_videos) {
            let urls = article_video.split('/')
            let old_url = path.join(path.resolve(), '/public/assets', 'video', urls[urls.length - 1])
            req.session.article_videos.push(old_url)
        }
        File.delete(req.session.article_videos, req.body.article_videos)
        req.session.article_videos = []

        return res.end(JSON.stringify({ code: 200 }))
    })

router
    .get('/api/article/:id', async (req, res) => {
        let result = await ArticleDao.selectById(req.params.id)
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        return res.end(JSON.stringify({ code: 200, data: result }))
    })

    .get('/api/articleByPage/:pageIndex/:pageSize', async (req, res) => {
        let { pageIndex, pageSize } = req.params

        let result = await ArticleDao.selectByLimit((pageIndex - 1) * pageSize, pageSize)
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        return res.end(JSON.stringify({ code: 200, data: result }))
    })

    .get('/api/articleTotal', async (req, res) => {
        let result = await ArticleDao.selectCount()
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        return res.end(JSON.stringify({ code: 200, data: result }))
    })

export default router